/**
 * 向流程事件关联人员发送消息
 */
def sendWFMessage(txt, receiver=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){
	
	try {
		
		if(snail.common.utils.StringHelper.isBlank(receiver)) {
			
			if(__WORKFLOW_EVENT_TYPE__ =~ /^__ON_BEFORE_.+/) {
				
				sendMessage(txt, com.seeyon.ctp.common.AppContext.currentUserId() as String);
				
			} else {
				
				def businessData = eventContext.getBusinessData();
				def formDataBean = businessData['formDataBean'];
				
				sendMessage(txt, formDataBean.startMemberId as String);
			}
			
		}else {
			sendMessage(txt, receiver);
		}
		
	} catch(Throwable e){
		
		logger("向流程事件关联人员发送消息失败！[TXT: ${txt}, ERROR: ${e.message}]");
	}
}


/**
 * 	从【流程事件】的环境对象中获取表单数据
 * @return
 */
def getFormData(eventContext=__WORKFLOW_EVENT_CONTEXT__) {
	
	def businessData = eventContext.getBusinessData();
	def formDataBean = businessData['formDataBean'];
	def formBean = businessData['formBean'];
	
	def formData = [:];
	
	// 提取主表数据 
	def mainTable = formDataBean.getFormTable();
	
	formData[mainTable.display] = ({_table, _data->
	
		def data = [:];
		data['ID'] = _data.id;
		
		_table.getFields().each{_formFieldBean->
			
			data[_formFieldBean.display] = _data.getFieldValue(_formFieldBean.name);
		}
		
		return data;
	
	})(mainTable, formDataBean);
	
	
	// 提取从表数据 
	def subTables = formBean.getSubTableBean();
	
	if(!ClassHelper.isBlank(subTables)) {
		
		subTables.each{_table->
		
			def dataList = formDataBean.getSubData(_table.tableName);
			
			if(ClassHelper.isBlank(dataList)) {
				
				formData[_table.display] = null;
				return ;
			}
			
			formData[_table.display] = ({_dataList, _table2->
			
				def dataList2 = [];
				
				_dataList.each{_data->
				
					def data = [:];
					data['ID'] = _data.id;
					
					_table2.getFields().each{_formFieldBean->
						
						data[_formFieldBean.display] = _data.getFieldValue(_formFieldBean.name);
					}
					
					dataList2 << data;
				}
				
				return dataList2;
				
			})(dataList, _table);
		}
	}
	
	return formData;
}


/**
 * 	从【流程事件】的环境对象中获取表单数据  (KEY 为 FIELDXXXX)
 * @return
 */
def getFormData2(eventContext=__WORKFLOW_EVENT_CONTEXT__) {
	
	def businessData = eventContext.getBusinessData();
	def formDataBean = businessData['formDataBean'];
	def formBean = businessData['formBean'];
	
	def formData = [:];
	
	// 提取主表数据 
	def mainTable = formDataBean.getFormTable();
	
	formData[mainTable.tableName] = ({_table, _data->
	
		def data = [:];
		data['id'] = _data.id;
		
		_table.getFields().each{_formFieldBean->
			
			data[_formFieldBean.name] = _data.getFieldValue(_formFieldBean.name);
		}
		
		return data;
	
	})(mainTable, formDataBean);
	
	
	// 提取从表数据 
	def subTables = formBean.getSubTableBean();
	
	if(!ClassHelper.isBlank(subTables)) {
		
		subTables.each{_table->
		
			def dataList = formDataBean.getSubData(_table.tableName);
			
			if(ClassHelper.isBlank(dataList)) {
				
				formData[_table.tableName] = null;
				return ;
			}
			
			formData[_table.tableName] = ({_dataList, _table2->
			
				def dataList2 = [];
				
				_dataList.each{_data->
				
					def data = [:];
					data['id'] = _data.id;
					
					_table2.getFields().each{_formFieldBean->
						
						data[_formFieldBean.name] = _data.getFieldValue(_formFieldBean.name);
					}
					
					dataList2 << data;
				}
				
				return dataList2;
				
			})(dataList, _table);
		}
	}
	
	return formData;
}


/* 从【流程事件】的环境对象中获取表单数据，并转换为时间 */
def getTime(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){
    
    def value = null;
    
	try {
		
		value = getValue(key, null, eventContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
			
			(0..<value.size()).each{i->
				
				try {
					
					if(value[i] instanceof String){
						value[i] = new java.sql.Timestamp(snail.common.utils.DateHelper.parse(value[i], 'yyyy-MM-dd HH:mm:ss').getTime());
					}
				} catch(Exception e){}
			}
			
		} else if(value instanceof String){
			value = new java.sql.Timestamp(snail.common.utils.DateHelper.parse(value, 'yyyy-MM-dd HH:mm:ss').getTime());
		}

	} catch(Exception e){
		logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return value;
}


/* 从【流程事件】的环境对象中获取表单数据，并转换为时间文本 */
def getTimeText(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){
    
    def value = null;
    
	try {
		
		value = getValue(key, null, eventContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
			
			(0..<value.size()).each{i->
				try {
					value[i] = ((null != value[i]) ? (new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value[i])) : '');
				} catch(Exception e){}
			}
			
		} else if(value instanceof java.util.Date){
			value = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value);
		}

	} catch(Exception e){
		logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return value;
}


/* 从【流程事件】的环境对象中获取表单数据，并转换为日期 */
def getDate(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){
    
    def value = null;
    
	try {
		
		value = getValue(key, null, eventContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
			
			(0..<value.size()).each{i->
				
				try {
					
					if(value[i] instanceof String){
						value[i] = snail.common.utils.DateHelper.parse(value[i]);
					}
				} catch(Exception e){}
			}
			
		} else if(value instanceof String){
			value = snail.common.utils.DateHelper.parse(value);
		}

	} catch(Exception e){
		logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return value;
}


/* 从【流程事件】的环境对象中获取表单数据，并转换为日期文本 */
def getDateText(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){
    
    def value = null;
    
	try {
		
		value = getValue(key, null, eventContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
			
			(0..<value.size()).each{i->
				try {
					value[i] = ((null != value[i]) ? (new java.text.SimpleDateFormat("yyyy-MM-dd").format(value[i])) : '');
				} catch(Exception e){}
			}
			
		} else if(value instanceof java.util.Date){
			value = new java.text.SimpleDateFormat("yyyy-MM-dd").format(value);
		}

	} catch(Exception e){
		logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return value;
}


/* 从【流程事件】的环境对象中获取表单数据，并转换为枚举显示值 */
def getEnumShowvalue(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){
    
    try {
        
        def enumManager = com.seeyon.ctp.common.AppContext.getBean("enumManagerNew");
        
        def id = getValue(key, defaultValue, eventContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                try {
                    def enumItem = enumManager.getEnumItem(new Long(id[i] as String));
                
                    names << ((null != enumItem) ? enumItem.getShowvalue() : '');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
            
        } else {
            def enumItem = enumManager.getEnumItem(new Long(id as String));
            
            return ((null != enumItem) ? enumItem.getShowvalue() : defaultValue);    
        }

    } catch(Exception e){
        logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【流程事件】的环境对象中获取表单数据，并转换为枚举值 */
def getEnumValue(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){
    
    try {

        def enumManager = com.seeyon.ctp.common.AppContext.getBean("enumManagerNew");
        
        def id = getValue(key, defaultValue, eventContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
                
                try {
                    def enumItem = enumManager.getEnumItem(new Long(id[i] as String));
                    
                    names << ((null != enumItem) ? enumItem.getEnumvalue() : '');
                } catch(Exception e){
                    names << '';
                }
                
            }
            
            return names;
            
        } else {
            def enumItem = enumManager.getEnumItem(new Long(id as String));
            
            return ((null != enumItem) ? enumItem.getEnumvalue() : defaultValue);    
        }
        
    } catch(Exception e){
        logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【流程事件】的环境对象中获取表单数据，并转换为枚举编码 */
def getEnumCode(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){
    
    try {

        def enumManager = com.seeyon.ctp.common.AppContext.getBean("enumManagerNew");
        
        def id = getValue(key, defaultValue, eventContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
                
                try {
                    def enumItem = enumManager.getEnumItem(new Long(id[i] as String));
                    
                    names << ((null != enumItem) ? enumItem.getEnumItemCode() : '');
                } catch(Exception e){
                    names << '';
                }
                
            }
            
            return names;
            
        } else {
            def enumItem = enumManager.getEnumItem(new Long(id as String));
            
            return ((null != enumItem) ? enumItem.getEnumItemCode() : defaultValue);    
        }
        
    } catch(Exception e){
        logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【流程事件】的环境对象中获取表单数据，并转换为姓名 */
def getMember(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){
    
    try {

        def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
        
        def id = getValue(key, defaultValue, eventContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                def _names = [];
                
                try {
                    
                    id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
                    
                    id[i].each{
                        
                        try {
                            def member = orgManager.getMemberById(new Long(it));
                        
                            _names << ((null != member) ? member.name : '');
                        } catch(Exception e){}
                    }
                    
                    names << _names.join(',');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
        } else {
            
            def names = [];
            
            id = (id as String).split(',');
            
            id.each{
                
                def member = orgManager.getMemberById(new Long(it));
                
                names << ((null != member) ? member.name : '');
            }
            
            return names.join(',');
        }
        
    } catch(Exception e){
        logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【流程事件】的环境对象中获取表单数据，并转换为人员编号 */
def getMemberCode(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){
    
    try {

        def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
        
        def id = getValue(key, defaultValue, eventContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                def _values = [];
                
                try {
                    
                    id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
                    
                    id[i].each{
                        
                        try {
                            def member = orgManager.getMemberById(new Long(it));
                        
                            _values << ((null != member) ? member.code : '');
                        } catch(Exception e){}
                    }
                    
                    names << _values.join(',');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
        } else {
            
            def names = [];
            
            id = (id as String).split(',');
            
            id.each{
                
                def member = orgManager.getMemberById(new Long(it));
                
                names << ((null != member) ? member.code : '');
            }
            
            return names.join(',');
        }
        
    } catch(Exception e){
        logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【流程事件】的环境对象中获取表单数据，并转换为人员登录名 */
def getLoginName(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){
    
    try {

        def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
        
        def id = getValue(key, defaultValue, eventContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                def _values = [];
                
                try {
                    
                    id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
                    
                    id[i].each{
                        
                        try {
                            def member = orgManager.getMemberById(new Long(it));
                        
                            _values << ((null != member) ? member.loginName : '');
                        } catch(Exception e){}
                    }
                    
                    names << _values.join(',');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
        } else {
            
            def names = [];
            
            id = (id as String).split(',');
            
            id.each{
                
                def member = orgManager.getMemberById(new Long(it));
                
                names << ((null != member) ? member.loginName : '');
            }
            
            return names.join(',');
        }
        
    } catch(Exception e){
        logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}

/* 从【流程事件】的环境对象中获取表单数据，并转换为部门名称 */
def getDeptName(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, eventContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _names = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def department = orgManager.getDepartmentById(new Long(it));
						
							_names << ((null != department) ? department.name : '');
						} catch(Exception e){}
					}
					
					names << _names.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def department = orgManager.getDepartmentById(new Long(it));
				
				names << ((null != department) ? department.name : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
    
    return defaultValue;
}

/* 从【流程事件】的环境对象中获取表单数据，并转换为部门编号 */
def getDeptCode(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, eventContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _values = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def department = orgManager.getDepartmentById(new Long(it));
						
							_values << ((null != department) ? department.code : '');
						} catch(Exception e){}
					}
					
					names << _values.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def department = orgManager.getDepartmentById(new Long(it));
				
				names << ((null != department) ? department.code : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
    
    return defaultValue;
}

/* 从【流程事件】的环境对象中获取表单数据，并转换为单位名称 */
def getUnitName(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, eventContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _names = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def unit = orgManager.getAccountById(new Long(it));
						
							_names << ((null != unit) ? unit.name : '');
						} catch(Exception e){}
					}
					
					names << _names.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def unit = orgManager.getAccountById(new Long(it));
				
				names << ((null != unit) ? unit.name : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}

/* 从【流程事件】的环境对象中获取表单数据，并转换为单位编号 */
def getUnitCode(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, eventContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _values = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def unit = orgManager.getAccountById(new Long(it));
						
							_values << ((null != unit) ? unit.code : '');
						} catch(Exception e){}
					}
					
					names << _values.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def unit = orgManager.getAccountById(new Long(it));
				
				names << ((null != unit) ? unit.code : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}

/* 从【流程事件】的环境对象中获取表单数据，并转换为岗位名称 */
def getPostName(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, eventContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _names = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def post = orgManager.getPostById(new Long(it));
						
							_names << ((null != post) ? post.name : '');
						} catch(Exception e){}
					}
					
					names << _names.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def post = orgManager.getPostById(new Long(it));
				
				names << ((null != post) ? post.name : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}

/* 从【流程事件】的环境对象中获取表单数据，并转换为岗位编号 */
def getPostCode(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, eventContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _values = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def post = orgManager.getPostById(new Long(it));
						
							_values << ((null != post) ? post.code : '');
						} catch(Exception e){}
					}
					
					names << _values.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def post = orgManager.getPostById(new Long(it));
				
				names << ((null != post) ? post.code : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}

/* 从【流程事件】的环境对象中获取表单数据，并转换为职务级别名称 */
def getLevelName(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, eventContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _names = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def level = orgManager.getLevelById(new Long(it));
						
							_names << ((null != level) ? level.name : '');
						} catch(Exception e){}
					}
					
					names << _names.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def level = orgManager.getLevelById(new Long(it));
				
				names << ((null != level) ? level.name : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}

/* 从【流程事件】的环境对象中获取表单数据，并转换为职务级别编号 */
def getLevelCode(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, eventContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _values = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def level = orgManager.getLevelById(new Long(it));
						
							_values << ((null != level) ? level.code : '');
						} catch(Exception e){}
					}
					
					names << _values.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def level = orgManager.getLevelById(new Long(it));
				
				names << ((null != level) ? level.code : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}


/* 从【流程事件】的环境对象中获取表单数据，并转换为数字 */
def getNumber(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){

	try {

		def value = getValue(key, defaultValue, eventContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
			
			def values = [];
			
			value.each{val->
				
				if(null == val){
				
					values << null;
					
				}else if(val instanceof java.lang.Number){
				
					values << val;
				
				} else {
				
					values << new java.math.BigDecimal(val as String);
				}
			}
			
			return values;
		} else if(value instanceof java.lang.Number){
			
			return value;
			
		} else {
			
			return new java.math.BigDecimal(value as String);
		}
		
	} catch(Exception e){
		logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}


/* 从【流程事件】的环境对象中获取表单数据，并转换为字符串 */
def getString(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){

	try {

		def value = getValue(key, defaultValue, eventContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
			
			def values = [];
			
			value.each{val->
				
				if(null == val){
				
					values << null;
					
				}else if(val instanceof java.lang.String){
				
					values << val;
				
				} else {
				
					values << (val as String);
				}
			}
			
			return values;
		} else if(value instanceof java.lang.String){
			
			return value;
			
		} else {
			
			return value as String;
		}
		
	} catch(Exception e){
		logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}


/* 从【流程事件】的环境对象中获取表单数据 */
def getValue(key, defaultValue=null, eventContext=__WORKFLOW_EVENT_CONTEXT__){
    
	try {
        
        def value = null;
    	
        def businessData = eventContext.getBusinessData();
        
        if(businessData.containsKey(key)) {
        	
        	value = businessData[key];
        	
        }else {

			def formDataBean = businessData['formDataBean'];
			def formBean = businessData['formBean'];
			
			if(!(key ==~ /field\d+/)) {
		        	
	        	def formFieldBean = formBean.getFieldBeanByDisplay(key);
	           
	        	if(null != formFieldBean) {
	        		
	        		key = formFieldBean.getName();
	        	}
	        }
	        
	        value = formDataBean.getFieldValue(key);
	        
	        if(null == value){
	            value = formDataBean.getAllDataMap().get(key);
	        }
        }
        
        return (null != value ? value : defaultValue);
        
    } catch(Exception e){
        logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 在【流程事件】的环境下，对当前表单数据进行更新 */
def setValue(key, value, sleeptime=200, eventContext=__WORKFLOW_EVENT_CONTEXT__){
	
	if(__WORKFLOW_EVENT_TYPE__.startsWith("__ON_BEFORE_")) {
		
		_setValueFromWorkflow(key, value, eventContext);
		
	}else {
		_setValueFromWorkflow2(key, value, sleeptime, eventContext);
	}
}

/* 在【流程事件】的环境下，对当前表单数据进行更新  (注：被更新字段不能为编辑态，因为编辑态下前端数据会覆盖后端数据)*/
def _setValueFromWorkflow(key, value, eventContext=__WORKFLOW_EVENT_CONTEXT__){
		
	try {
		
		def businessData = eventContext.getBusinessData();
		def formDataBean = businessData['formDataBean'];
		def formBean = businessData['formBean'];
    	def formFieldBean = formBean.getFieldBeanByDisplay(key);
    	
    	if(null == formFieldBean){
    		formFieldBean = formBean.getFieldBeanByName(key);
    	}
        
		if(null == formFieldBean){
			logger("在【流程事件】的环境下，${key}不存在！");
			return;
		}

		def fieldName = formFieldBean.getName();
		def tableName = formFieldBean.getOwnerTableName();
		
		def sql = '';
		def updataSize = 0;
		
		
		if(formFieldBean.isMasterField()) { // 主表
			
			formDataBean.addFieldValue(fieldName, ((value instanceof java.util.List && 0 < value.size()) ? value[0] : value));
			
		} else { // 从表
			        
			def subFormDataBeanList = formDataBean.getSubData(tableName);
			def subFormDataBeanListSize = subFormDataBeanList.size();
			
			if(subFormDataBeanListSize != value.size()) {
				
				logger("从表数据行数与传入的数据行数不匹配！【${subFormDataBeanListSize} -> ${value.size()}】");
			}

			subFormDataBeanListSize = (subFormDataBeanListSize < value.size() ? subFormDataBeanListSize : value.size());
			
			for(int i=0; i<subFormDataBeanListSize; i++){
				
				subFormDataBeanList[i].addFieldValue(fieldName, value[i]);
			}
		}
		
    } catch(Exception e){
        logger("在【流程事件】的环境下，对当前表单数据进行更新失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
}


/* 在【流程事件】的环境下，对当前表单数据进行更新 (XXX后事件) */
def _setValueFromWorkflow2(key, value, sleeptime=200, eventContext=__WORKFLOW_EVENT_CONTEXT__){
		
	try {
	
		def businessData = eventContext.getBusinessData();
		def formData = businessData['formDataBean'];
    	def formBean = businessData['formBean'];
    	
    	def formFieldBean = formBean.getFieldBeanByDisplay(key);
    	
    	if(null == formFieldBean){
    		
    		formFieldBean = formBean.getFieldBeanByName(key);
    	}
        
		if(null == formFieldBean){
			
			logger("在【触发器】的环境下，${key}不存在！");
			
			return;
		}

		
		def id = formData.getId();
		def fieldName = formFieldBean.getName();
		def tableName = formFieldBean.getOwnerTableName();
		
		def sql = ('UPDATE ' + tableName + ' SET ' + fieldName + '=? WHERE id=?');
		def dataList = [];
		
		if(formFieldBean.isMasterField()) { // 主表
			
			dataList << [((value instanceof java.util.List && 0 < value.size()) ? value[0] : value), id];
			
		} else { // 从表
			
			def subFormData = formData.getSubData(tableName);
        
			if(value.size() != subFormData.size()) {
				
				logger("从表数据行数与传入的数据行数不匹配！【${subFormData.size()} -> ${value.size()}】");
			}
			
			def subFormDataSize = (subFormData.size() < value.size() ? subFormData.size() : value.size());
			
			for(int i=0; i<subFormDataSize; i++){

				dataList << [value[i], subFormData[i].id];
			}
		}

		logger('TRIGGER_WORKFLOW_UPDATE_FORMDATA SQL >> ' + JSONHelper.stringify([sql, dataList]));
		
		Thread.start {
			
			// 延迟200毫秒
			sleep(sleeptime);
			
			def sqlClient = null;
			def sqlTxt = sql;
			def sqlDataList = dataList;
			
			try {
				
				// 执行SQL		
				sqlClient = getSQLClient();
				
				sqlClient.withTransaction{
					
					sqlDataList.each{_sqlData->
						
						def updataSize = sqlClient.executeUpdate(sql, _sqlData);
						
						if(1 != updataSize) {
							
							logger("在【流程事件】的环境下，对当前表单数据进行更新可能失败了！SQL: ${sqlTxt}, VALUE: ${_sqlData[0]}, ID: ${_sqlData[1]}");
						}
					}
				}
				
			}finally{
				
				if(null != sqlClient){
					try{
						sqlClient.close();
					} catch(Throwable e){}
				}
			}
			
		}
    } catch(Exception e){
        logger("在【流程事件】的环境下，对当前表单数据进行更新失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
}




